<?php
/**
 * Desc: Service模板
 * User: zdb
 * Date: 2023/6/5
 * Time: 11:22
 */
namespace app\api\service\develop;
use think\facade\Db;

class DatagridToolbarService
{
    private $primaryTable;
    const ARR_TOOLBAR_INFO = array(
        'add' => array('button_text' => '新增', 'button_icon' => 'fa fa-plus', 'button_class'=>'tool-btn-add', 'is_need_check' => 0),
        'edit' => array('button_text' => '编辑', 'button_icon' => 'fa fa-edit', 'button_class'=>'tool-btn-edit', 'is_need_check' => 1),
        'delete' => array('button_text' => '删除', 'button_icon' => 'fa fa-trash', 'button_class'=>'tool-btn-delete', 'is_need_check' => 1),
        'refresh' => array('button_text' => '刷新', 'button_icon' => 'fa fa-refresh', 'button_class'=>'tool-btn-refresh', 'is_need_check' => 0),
    );

    public function __construct()
    {
        $this->primaryTable = "yj_datagrid_toolbar";
    }

    public function findRowById($id)
    {
        return Db::table($this->primaryTable)->alias('a')
            ->where('id', '=', $id)
            ->strict(false)
            ->find();
    }

    public function findRowByData($data)
    {
        $condition = $this->assembleFilterCondition($data);
        return Db::table($this->primaryTable)->alias('a')
            ->where($condition)
            ->strict(false)
            ->find();
    }

    public function getListByManyId($arrId)
    {
        $list = Db::table($this->primaryTable)->alias('a')
            ->whereIn('id', $arrId)
            ->select()
            ->toArray();
        return $list;
    }

    public function assembleFilterCondition($data)
    {
        $condition = ['a.status > 0'];

        if (!empty($data['title'])) {
            $condition[] = "title like '%" . $data['title'] . "%'";
        }

        if (!empty($data['datagrid_id'])) {
            $condition[] = "a.datagrid_id = " . $data['datagrid_id'];
        }

        if (!empty($data['button_type'])) {
            $condition[] = "a.button_type = '" . $data['button_type']."'";
        }

        if (!empty($data['status'])) {
            $condition[] = "a.status = " . $data['status'];
        }

        return empty($condition) ? "" : implode(" AND ", $condition);
    }

    public function getListNoPage($data = [])
    {
        $condition = $this->assembleFilterCondition($data);
        $res = Db::table($this->primaryTable)->alias('a')
            ->where($condition)
            ->select()
            ->toArray();
        return $res;
    }

    public function getListWithPage($data, $pageIndex = 1, $pageSize = 30)
    {
        $offset = ($pageIndex - 1) * $pageSize;
        $condition = $this->assembleFilterCondition($data);
        $res = Db::table($this->primaryTable)
            ->where($condition)
            ->limit($offset, $pageSize)
            ->order('id desc')
            ->select();
        return $res;
    }

    public function getTotalNum($data)
    {
        $condition = $this->assembleFilterCondition($data);
        $num = Db::table($this->primaryTable)->alias('a')
            ->where($condition)
            ->count();
        return $num;
    }

    public function createRow($data)
    {
        $data['status'] = isset($data['status']) ? $data['status']:1;
        $res = Db::table($this->primaryTable)
            ->strict(false)
            ->insert($data, true);
        return $res;
    }

    public function createManyRow($data)
    {
        $res = Db::table($this->primaryTable)
            ->strict(false)
            ->duplicate($data)
            ->insertAll($data, true);
        return $res;
    }

    public function updateRowById($id, $data)
    {
        $res = Db::table($this->primaryTable)->alias('a')
            ->where('id', '=', $id)
            ->strict(false)
            ->save($data);
        return false !== $res ? true : false;
    }

    public function updateListByData($condition, $data)
    {
        $condition = $this->assembleFilterCondition($data);
        $res = Db::table($this->primaryTable)->alias('a')
            ->where($condition)
            ->save($data);
        return false !== $res ? true : false;
    }

    public function updateListByManyId($arrId, $data)
    {
        $res = Db::table($this->primaryTable)->alias('a')
            ->whereIn('a.id', $arrId)
            ->strict(false)
            ->save($data);
        return false !== $res ? true : false;
    }

    public function removeRowById($id)
    {
        $data = ['status' => 0];
        $res = Db::table($this->primaryTable)
            ->where('id', '=', $id)
            ->strict(false)
            ->save($data);
        return false !== $res ? true : false;
    }

    public function removeListByManyId($arrId)
    {
        $data = ['status' => 0];
        $res = Db::table($this->primaryTable)->alias('a')
            ->whereIn('a.id', $arrId)
            ->strict(false)
            ->save($data);
        return false !== $res ? true : false;
    }

    public function removeListByData($data)
    {
        $condition = $this->assembleFilterCondition($data);
        $data = ['status' => 0];
        $res = Db::table($this->primaryTable)->alias('a')
            ->where($condition)
            ->save($data);
        return false !== $res ? true : false;
    }

    public function getPageAllConfigureApi($data)
    {
        $optionList = $this->getListNoPage($data);
        $finalData = array();
        foreach ($optionList as $item) {
            if('add' == $item['button_type']){
                $finalData['path_page_add'] = $item['request_page_path'];
                $finalData['path_api_add'] = $item['request_api_path'];
            }else if('edit' == $item['button_type']){
                $finalData['path_page_edit'] = $item['request_page_path'];
                $finalData['path_api_edit'] = $item['request_api_path'];
            }else if('delete' == $item['button_type']){
                $finalData['path_page_delete'] = $item['request_page_path'];
                $finalData['path_api_delete'] = $item['request_api_path'];
            } else if('refresh' == $item['button_type']){
                $finalData['path_page_get'] = $item['request_page_path'];
                $finalData['path_api_get'] = $item['request_api_path'];
            }
        }
        return $finalData;
    }

    public function getDatagridRefreshApi($data)
    {
        $data['button_type'] = "refresh";
        $optionList = $this->getListNoPage($data);
        $optionItem = $optionList[0] ?? array();
        $apiPath = $optionItem['request_api_path'] ?? '';
        return $apiPath;
    }
}
